//
//  CXBaseViewController.m
//  Cxzc
//
//  Created by 张露杰 on 17/8/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//


#import "HWDBaseViewController.h"
@interface HWDBaseViewController ()
@end
@implementation HWDBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Configurations

- (void)setupBackBUtton {
    if (!self.navigationController) {
        return;
    }
    if (self != self.navigationController.viewControllers[0] && [self needShowBackButton]) {
        [self initBackButton];
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

- (BOOL)needShowBackButton {
    return YES;
}

- (BOOL)hidesBottomBarWhenPushed {
    if (self != self.navigationController.viewControllers[0]) {
        return YES;
    }
    return NO;
}

- (CGSize)imageItemSize {
    return CGSizeMake(44,44);
}

/**
 *  计算导航栏左右两边item的宽度
 */
- (CGFloat)itemViewWidth {
    CGRect leftItemBounds = self.navigationItem.leftBarButtonItem.customView.bounds;
    // 这一步转换是为了得到barbutton在navigationbar上的frame
    CGRect convertFrame = [self.navigationItem.leftBarButtonItem.customView convertRect:leftItemBounds toView:self.navigationController.navigationBar];
    // Note: 这个padding的值就是barbutton与导航栏左侧的边距，6plus上这个值是20，4~6都是16，6plus上得到的值是其它小尺寸设备的1.25倍。这里按标准边距来处理
    CGFloat padding = convertFrame.origin.x;
    
    CGRect rightItemBounds = self.navigationItem.rightBarButtonItem.customView.bounds;
    CGFloat width = MAX(leftItemBounds.size.width, rightItemBounds.size.width) + padding;
    return width;
}

#pragma mark - Init Views

-(void)initTitleWithTitle:(NSString *)title
{
//    UILabel *titleLabel = [TomamaUI createLabel:CGRectMake(0, 0, [TomamaUI getStringWeithtWith:title andHeigth:20 andFont:[UIFont systemFontOfSize:19*screenW_6]], 20) textAligment:1 font:[UIFont systemFontOfSize:19*screenW_6] textColor:TextW text:title];
//    self.navigationItem.titleView = titleLabel;
}

- (void)initBackButton {
    [self initLeftBarButtonWithImage:@"back"];
}

- (void)initLeftBarButtonWithImage:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button addTarget:self action:@selector(onTapLeftBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButtonitem;
}

- (void)initRightBarButtonWithImage:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(onTapRightBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButtonitem;
}

- (void)initLeftBarButtonWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor andFont:(CGFloat)font {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(onTapLeftBarButton)];
    self.navigationItem.leftBarButtonItem = barButton;
    [self.navigationItem.leftBarButtonItem setTintColor:titleColor];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

- (void)initRightBarButtonWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor andFont:(CGFloat)font{
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(onTapRightBarButton)];
    self.navigationItem.rightBarButtonItem = barButton;
    [self.navigationItem.rightBarButtonItem setTintColor:titleColor];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

-(void)initRightBarButtonsWithImageArray:(NSArray *)array
{
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 0, 30, 30);
    [button1 setImage:[UIImage imageNamed:array[0]] forState:UIControlStateNormal];
    button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button1 addTarget:self action:@selector(onTapRightBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, 0, 30, 30);
    [button2 setImage:[UIImage imageNamed:array[1]] forState:UIControlStateNormal];
    button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button2 addTarget:self action:@selector(onTapRightBarButton2) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonitem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
    
    NSArray *buttons = [NSArray arrayWithObjects:barButtonitem1,barButtonitem2, nil];
    self.navigationItem.rightBarButtonItems = buttons;
}

#pragma mark - Event

- (void)onTapLeftBarButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onTapRightBarButton {
    
}

- (void)onTapRightBarButton2 {
    
}

- (void)segmentControlClicked:(UISegmentedControl *)segmentControl {
    
}

- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

- (void)dealloc {
    NSLog(@"销毁");
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end
